package com.jackie.tongzhuang;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jackie.tongzhuang.Adapters.FenQiCardAdapter;
import com.jackie.tongzhuang.Classes.BaseClass;
import com.jackie.tongzhuang.Classes.BusinessClass;
import com.jackie.tongzhuang.Controls.FenQiList;
import com.jackie.tongzhuang.Controls.GridRadioGroup;
import com.jackie.tongzhuang.Controls.MyListView;
import com.jackie.tongzhuang.Controls.OptionItem;
import com.jackie.tongzhuang.Controls.TitleBar;
import com.jackie.tongzhuang.DataBase.DbClass;
import com.jackie.tongzhuang.Enums.QiShu;
import com.jackie.tongzhuang.Models.FenQi;

import java.util.ArrayList;
import java.util.List;

public class HuanKuanSubmitActivity extends AppCompatActivity {

    private TextView jinetext,yixuantext,weixuantext;
    private LinearLayout yixuanlist,weixuanlist,wxlay,fqlay;
    private TitleBar titleBar;
    private GridRadioGroup group;

    private DbClass dbClass = new DbClass(this);
    private double yxznum=0,wxznum=0,bqnum=0;
    private int fqs;
    private ArrayList<Integer> noselect,select;
    private List<FenQi> zfqls = new ArrayList<>();
    private List<FenQi> zzfqls = new ArrayList<>();
    private double kyed = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_submit_huankuan);
        initView();

        noselect = getIntent().getIntegerArrayListExtra("noselect");
        select = getIntent().getIntegerArrayListExtra("select");

        group.setColumnNum(3);


        //已选择偿还分期列表
        for(Integer i : select){
            FenQi fq = dbClass.getFenQiById(i);
            FenQiList fql = new FenQiList(this,null);
            fql.setId(fq.getId());
            fql.setJinE(BaseClass.getDecimalToStr(2,fq.getFenQiJinE()));
            fql.setShowcheck(false);
            fql.setBeiZhu(fq.getFenQiRiQi()+" "+fq.getJinHuoDan().getGongHuoShang().getMingCheng()+" 分期 第 "+fq.getQiShu()+" 期");
            fql.setRiQi(fq.getChangHuanRiQi());
            yxznum += fq.getFenQiJinE();
            yixuanlist.addView(fql);
        }

        if(noselect.size()>0){
            wxlay.setVisibility(View.VISIBLE);
            //未选择偿还分期列表
            for(Integer i : noselect){
                FenQi fq = dbClass.getFenQiById(i);
                FenQiList fql = new FenQiList(this,null);
                fql.setId(fq.getId());
                fql.setJinE(BaseClass.getDecimalToStr(2,fq.getFenQiJinE()));
                fql.setShowcheck(false);
                fql.setBeiZhu(fq.getFenQiRiQi()+" "+fq.getJinHuoDan().getGongHuoShang().getMingCheng()+" 分期 第 "+fq.getQiShu()+" 期");
                fql.setRiQi(fq.getChangHuanRiQi());
                wxznum += fq.getFenQiJinE();
                weixuanlist.addView(fql);
            }
        }else{
            wxlay.setVisibility(View.GONE);
        }
        initOption();

        yixuantext.setText("¥ "+BaseClass.getDecimalToStr(2,yxznum));
        weixuantext.setText("¥ "+BaseClass.getDecimalToStr(2,wxznum));
        jinetext.setText("¥ "+BaseClass.getDecimalToStr(2,(yxznum+wxznum)));
        titleBar.setText("确认还款");
        titleBar.setOnClickedListener(new TitleBar.TitleBarClickListener() {
            @Override
            public void backClick(View v) {
                finish();
            }

            @Override
            public void submitClick(View v) {

                //更新所有分期数据
                for(Integer i: select){
                    FenQi sfq = dbClass.getFenQiById(i);
                    ContentValues values = new ContentValues();
                    values.put("YiChangHuan",1);
                    values.put("ChangHuanJine",sfq.getFenQiJinE());
                    dbClass.update("FenQi",values,sfq.getId());
                }

                //插入再分期数据
                for(FenQi fq : zzfqls){
                    ContentValues fq_values = new ContentValues();
                    fq_values.put("JinHuoDanId",fq.getJinHuoDanId());
                    fq_values.put("QiShu",fq.getQiShu());
                    fq_values.put("YiChangHuan",fq.getYiChangHuan());
                    fq_values.put("FenQiJinE",fq.getFenQiJinE());
                    fq_values.put("BenJin",fq.getBenJin());
                    fq_values.put("LiXi",fq.getLiXi());
                    fq_values.put("FenQiRiQi",fq.getFenQiRiQi());
                    fq_values.put("ChangHuanRiQi",fq.getChangHuanRiQi());
                    dbClass.insert("FenQi",fq_values);
                }

                //删除未选择分期数据
                for(Integer i : noselect){
                    dbClass.delete("FenQi",i);
                }

                //更新可用额度
                kyed = dbClass.getYinHangKa(1).getKeYongEDu();
                ContentValues yhk_values = new ContentValues();
                yhk_values.put("KeYongEDu",kyed+yxznum);
                dbClass.update("YinHangKa",yhk_values,1);
                finish();
            }
        });

    }

    private void initView(){
        titleBar = findViewById(R.id.hk_submit_titlebar);
        jinetext = findViewById(R.id.hk_submit_hke);
        yixuantext = findViewById(R.id.hk_submit_yxhke);
        yixuanlist = findViewById(R.id.hk_submit_yxlist);
        weixuantext = findViewById(R.id.hk_submit_wxhke);
        weixuanlist = findViewById(R.id.hk_submit_wxlist);
        fqlay = findViewById(R.id.hk_submit_fqlist);
        wxlay = findViewById(R.id.hk_submit_wxlay);
        group = findViewById(R.id.hk_submit_fqgroup);
    }

    private void initOption(){
        for (QiShu qs : QiShu.values()) {
            OptionItem qo = new OptionItem(this,null);
            qo.setValue(qs.getValue());
            qo.setText(qs.getText());
            qo.setOnSelectedListener(new OptionItem.OnSelectedListener() {
                @Override
                public void onSelected(int sel_val) {
                    fqs = sel_val;
                    fqlay.removeAllViews();
                    zzfqls.clear();
                    if(sel_val != -1){
                        //开始计算分期数据，并显示在listView中
                        //依次遍历未选偿还分期中的每一个进行再次分期
                        for(Integer i : noselect){
                            FenQi fq = dbClass.getFenQiById(i);
                            zfqls = BusinessClass.getZaiFenQi(fq.getFenQiJinE(),fqs,fq.getQiShu(),fq.getJinHuoDanId());
                            zzfqls.addAll(zfqls);
                            TextView fqtitle = new TextView(HuanKuanSubmitActivity.this);
                            fqtitle.setText(fq.getFenQiRiQi()+" "+fq.getJinHuoDan().getGongHuoShang().getMingCheng()+" 分期 第 "+fq.getQiShu()+" 期");
                            fqtitle.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);



                            MyListView fqlist = new MyListView(HuanKuanSubmitActivity.this,null);
                            FenQiCardAdapter fqcAdapter = new FenQiCardAdapter(HuanKuanSubmitActivity.this,zfqls);
                            fqlist.setAdapter(fqcAdapter);
                            fqlay.addView(fqtitle);
                            fqlay.addView(fqlist);

                        }

                    }
                }
            });
            LinearLayout.LayoutParams paramsRb = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            group.addView(qo, paramsRb);
        }
    }
}